<?php
/**
 * Joomla! 1.5 component ArticleCashRewards
 *
 * @version $Id: topearners.php 2009-05-10 01:47:01 svn $
 * @author Maverick
 * @package Joomla
 * @subpackage components.models
 * @link http://joomladev.boddunan.com
 * @license GNU/GPL
 *
 * Review and approve the articles and assign points and cash rewards.
 * Users can see their articles and the points/cash rewards assigned in the front-end module.
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');
require_once(JPATH_SITE.DS.'components'.DS.'com_content'.DS.'helpers'.DS.'route.php');
/**
 * ArticleCashRewards Component ArticleCashRewards Model
 *
 * @author      notwebdesign
 * @package		Joomla
 * @subpackage	ArticleCashRewards
 * @since 1.5
 */
class ArticleCashRewardsModelTopEarners extends JModel {
	var $_total = null;
	var $_pagination = null;

	function __construct(){
	    parent::__construct();
	

      global $mainframe, $option;
      $config = JFactory::getConfig();
      // Get the pagination request variables
      $this->setState('limit', $mainframe->getUserStateFromRequest($option.'.limit', 'limit', $config->getValue('config.list_limit'), 'int'));
      $this->setState('limitstart', JRequest::getVar('limitstart', 0, '', 'int'));

      // In case limit has been changed, adjust limitstart accordingly
      $this->setState('limitstart', ($this->getState('limit') != 0 ? (floor($this->getState('limitstart') / $this->getState('limit')) * $this->getState('limit')) : 0));
	}

	function getData(){
  	
	  	global $mainframe, $option;;
	    // Get a database object
		$db =& JFactory::getDBO();
		$user =& JFactory::getUser();
		
		$orderby 	= ' ORDER BY au.cash DESC';

		$query = 'SELECT u.username, au.points, au.cash FROM '. TABLE_ACR_USERS .
            ' au LEFT JOIN #__users u on au.id=u.id' . $orderby;
		
		$db->setQuery($query, $this->getState('limitstart'), $this->getState('limit'));
		$rows = $db->loadObjectList();
	
		return $rows;
	}

	function getTotal(){
	    // Load the content if it doesn't already exist
	    if (empty($this->_total)) {
		    
		    $user =& JFactory::getUser();
		    $db =& JFactory::getDBO();
		    
	        $query = 'SELECT count(*) FROM #__content a WHERE a.created_by = '.
                $user->id.' AND (a.state=1 OR a.state=0)';
	        $db->setQuery( $query );
	        
	        $this->_total = $db->loadResult();    
	    }
	    
	    return $this->_total;
	}
	function getPagination(){
	    // Load the content if it doesn't already exist
	    if (empty($this->_pagination)) {
	        jimport('joomla.html.pagination');
	        $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
	    }
	    return $this->_pagination;
	}

}
?>